/*
@Author   : Uyynot
@Email    : uyynot@qq.com
@Time     : 2023/10/16 13:46
@File     : test.go
@Project  : GoProject
@Desc     :
*/
package main

import (
	"fmt"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"io"
	"net/http"
	"os"
)

func InitLogger() {
	var err error
	var logger *zap.Logger
	//实例化logger
	logger, err = zap.NewProduction()
	if err != nil {
		fmt.Printf("初始化出错：", err)
	}
	fmt.Printf("", logger)
	logger.Debug("调试模式")
	resp, err := http.Get("baidu.com")
	fmt.Printf("resp", resp)

	logger.Info("info模式", zap.String("source", "百度"), zap.Int("year", 2050), zap.Error(err))
	logger.Error("error模式")
}

func InitSugarLogger() {

	//实例化logger
	logger, err := zap.NewProduction()
	if err != nil {
		fmt.Printf("初始化出错：", err)
	}
	fmt.Printf("", logger)
	var sugarLogger *zap.SugaredLogger
	sugarLogger = logger.Sugar()
	sugarLogger.Info(struct {
		Name string
		Age  int
	}{Name: "3333", Age: 33333})
	resp, err := http.Get("baidu.com")
	fmt.Printf("resp", resp)
	sugarLogger.Errorf("err=%s", err)

}

func InitDiyLogger() {
	//修改编码器配置
	encoderConfig := zap.NewProductionEncoderConfig()
	//修改时间戳为2023-10-17T17:05:19.971+0800
	encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
	//日志等级修改为大写info->INFO
	encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
	//实例化编码器,采用默认配置(如何写入日志)
	encoder := zapcore.NewJSONEncoder(encoderConfig)
	//encoder := zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig())
	//encoder := zapcore.NewConsoleEncoder(zap.NewProductionEncoderConfig())
	//实例化syncer（指定日志写到哪儿）
	file, _ := os.Create("test.log")
	//利用io.Multiwriter支持多终端输出(文件/控制台)
	ws := io.MultiWriter(file, os.Stdout)
	writeSyncer := zapcore.AddSync(ws)
	//writeSyncer := zapcore.AddSync(file)
	//实例化默认日志级别
	logLevel := zapcore.DebugLevel

	//实例化日志
	core1 := zapcore.NewCore(encoder, writeSyncer, logLevel)

	err_file, _ := os.Create("err_test.log")
	err_core := zapcore.NewCore(encoder, zapcore.AddSync(err_file), zap.ErrorLevel)
	//logger := zap.New(core)
	core := zapcore.NewTee(core1, err_core)
	//增加caller调用者
	logger := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))
	//logger := zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))
	sugarLogger := logger.Sugar()

	sugarLogger.Debug("牛逼")
	sugarLogger.Infof("desc=%s", "可厉害了")
	sugarLogger.Warnf("warn=%s", "警告了")
	sugarLogger.Errorf("errs=%s", "都错了")
}
func main() {
	//InitLogger()
	//InitSugarLogger()
	InitDiyLogger()
}
